﻿/*Description:二维码转换
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Wangjh 
'create Date:20211028
'Rewriter:
'Rewrite Date:
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QRCoder;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using DatabaseAccess.DBUtility;

public partial class forms_Custom_my_testQrCode2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }

    #region 生成二维码
    /// <summary>
    /// 生成二维码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    protected void CreateQRCodeImg(string str)
    {

      
        string strCode = System.Web.HttpUtility.UrlEncode(str);//str;
        //转化为64位

        QRCodeGenerator qrGenerator = new QRCodeGenerator();

        QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, QRCodeGenerator.ECCLevel.L         );//Convert.ToBase64String ( Encoding.Default.GetBytes(str))
        QRCode qrcode = new QRCode(qrCodeData);

        Bitmap qrCodeImage = qrcode.GetGraphic(6, Color.Black, Color.White, null, 5, 1, false);
        MemoryStream ms = new MemoryStream();


        //qrCodeImage.Save(@"C:\Program Files (x86)\Camstar\InSite Web Application\InSiteWebApplication\ImageTemp\123.jpg");

        qrCodeImage.Save(ms, ImageFormat.Jpeg );

         // '响应类型
        Response.Clear();//清除所有缓存区的内容
        Response.ContentEncoding = Encoding.Default  ;//输出内容的编码为默认编码
        Context.Response.ContentType = "image/Jpeg";
       // '输出字符流
        Context.Response.BinaryWrite(ms.ToArray ());
        Response.End();//将当前所有缓冲区的输出内容发送到客户端，并停止页面的执行
    }
    #endregion

    //protected void btn_Click(object sender, EventArgs e)
    //{
    //    CreateQRCodeImg(txtInput.Text);
    //}
    //protected void btn2_Click(object sender, EventArgs e)
    //{
    //    byte[] c = Convert.FromBase64String(txtInput .Text );
    //   string a = System.Text.Encoding.Default.GetString(c);
    //   txtResult.Text = a;
    //}
    protected void btn3_Click(object sender, EventArgs e)
    {
        var bytes = Encoding.Default.GetBytes(txtInput .Text );
        txtResult.Text = bytes.Length.ToString() ;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //处理clob字段测试

        var exuteEntity = new ExcuteEntity("WritetoOtherSystem", ExcuteEntity.ExcuteTypeEnum.insert );
        var excuteFileds = new List<FieldEntity>() {new FieldEntity("WritetoOtherSystemID", Guid.NewGuid().ToString (), FieldEntity.FieldTypeEnum.Str),
                                                                         new FieldEntity("SendXml",@"<img src="""" />as手动<Image>R8<L></L></Image>
", FieldEntity.FieldTypeEnum.Clob  )};
        exuteEntity.ExcuteFileds = excuteFileds;

        DatabaseHelper.ExecuteDataByEntity(exuteEntity);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string str = CompressString(txtInput.Text);
        CreateQRCodeImg(str);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
      string a=  DecompressString(txtInput.Text);
        txtResult.Text = a;
    }
    /// <summary>
    /// 压缩字符串
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string CompressString(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);

        using (MemoryStream ms = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true))
            {
                gzip.Write(buffer, 0, buffer.Length);
            }

            byte[] compressed = ms.ToArray();
            return Convert.ToBase64String(compressed);
        }
    }
    /// <summary>
    /// 解压字符串
    /// </summary>
    /// <param name="compressedBase64"></param>
    /// <returns></returns>
    public static string DecompressString(string compressedBase64)
    {
        byte[] compressed = Convert.FromBase64String(compressedBase64);

        using (MemoryStream ms = new MemoryStream(compressed))
        {
            using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
            {
                using (StreamReader reader = new StreamReader(gzip, Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}